package com.cn.ycy.controller;

import com.cn.ycy.dto.ReturnT;
import com.cn.ycy.dto.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("html")
public class HtmlController {

    @ResponseBody
    /**
     * 直接返回html
     * 模拟一个表单
     *
     * @return
     */
    @RequestMapping(value = "/form", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String form() {
        log.info("userId {}", -1);
        User user = new User();
        user.setUsername("user");
        user.setUserId(-1);
        return "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head> \n" +
                "<meta charset=\"utf-8\"> \n" +
                "<title>菜鸟教程(runoob.com)</title> \n" +
                "</head>\n" +
                "<body>\n" +
                "\n" +
                "<form name=\"input\" action=\"http://127.0.0.1:8081/html/submitform\" method=\"post\">\n" +
                "Username: <input type=\"text\" name=\"user\">\n" +
                "<input type=\"submit\" value=\"Submit\">\n" +
                "</form>\n" +
                "\n" +
                "</body>\n" +
                "</html>";
    }

    @RequestMapping(value = "/submitform", method = RequestMethod.POST, consumes = "application/x-www-form-urlencoded;charset=UTF-8")
    @ResponseBody
    public ReturnT<Map<String, String>> submitform(@RequestParam Map<String, String> formvariable) {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.putAll(formvariable);
        return new ReturnT<>(stringStringHashMap);
    }

}
